package com.agriculture.monitor.service.impl;


import com.agriculture.monitor.dto.PositionPage;
import com.agriculture.monitor.dto.ValueLabelTree;
import com.agriculture.monitor.entity.Position;
import com.agriculture.monitor.entity.User;
import com.agriculture.monitor.mapper.PositionMapper;
import com.agriculture.monitor.service.IPositionService;
import com.agriculture.monitor.util.TreeUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务类
 * </p>
 *
 * @author admin
 * @since 2021-07-15
 */
@Service
public class IPositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements IPositionService {

    @Override
    @Transactional
    public Position saveData(Position position) {
        if (position.getId() == null) {
            String address = position.getAddress1();
            String[] s = address.split("/");
            Position create = new Position();
            create.setAddress1(s[0].trim());
            create.setAddress2(s[1].trim());
            create.setAddress3(s[2].trim());
            create.setAddressCode(position.getAddressCode());
            create.setAddressLast(position.getAddressLast());
            create.setCreateTime(LocalDateTime.now());
            baseMapper.insert(create);
        } else {
            String address = position.getAddress1();
            String[] s = address.split("/");
            Position create = new Position();
            create.setAddress1(s[0].trim());
            create.setAddress2(s[1].trim());
            create.setAddress3(s[2].trim());
            create.setAddressCode(position.getAddressCode());
            create.setAddressLast(position.getAddressLast());
            create.setCreateTime(LocalDateTime.now());
            create.setId(position.getId());
            create.setUpdateTime(LocalDateTime.now());
            baseMapper.updateById(create);
        }
        return position;
    }

    @Override
    public List<ValueLabelTree> tree() {
        List<Position> positionList = this.list();

        List<ValueLabelTree> valueLabelTrees = new ArrayList<>();

        Map<String, Integer> data = new HashMap<>();

        for (Position position : positionList) {
            if (!data.containsKey(position.getAddress1())) {
                data.put(position.getAddress1(), 1);
                String code = position.getAddressCode().split(",")[0];
                ValueLabelTree valueLabelTree = new ValueLabelTree(code, position.getAddress1());
                valueLabelTree.setId(code);
                valueLabelTree.setParentId(null);
                valueLabelTrees.add(valueLabelTree);
            }
        }

        for (Position position : positionList) {
            if (!data.containsKey(position.getAddress2())) {
                data.put(position.getAddress2(), 2);
                String parentId = position.getAddressCode().split(",")[0];
                String code = position.getAddressCode().split(",")[1];
                ValueLabelTree valueLabelTree = new ValueLabelTree(code, position.getAddress2());
                valueLabelTree.setId(code);
                valueLabelTree.setParentId(parentId);
                valueLabelTrees.add(valueLabelTree);
            }
        }

        for (Position position : positionList) {
            if (!data.containsKey(position.getAddress3())) {
                data.put(position.getAddress3(), 3);
                String parentId = position.getAddressCode().split(",")[1];
                String code = position.getAddressCode().split(",")[2];
                ValueLabelTree valueLabelTree = new ValueLabelTree(code, position.getAddress3());
                valueLabelTree.setId(code);
                valueLabelTree.setParentId(parentId);
                valueLabelTrees.add(valueLabelTree);
            }
        }

        for (Position position : positionList) {
            String parentId = position.getAddressCode().split(",")[2];
            ValueLabelTree valueLabelTree = new ValueLabelTree(position.getId().toString(), position.getAddressLast());
            valueLabelTree.setId(position.getId().toString());
            valueLabelTree.setParentId(parentId);
            valueLabelTrees.add(valueLabelTree);
        }

        return TreeUtils.buildTree(valueLabelTrees, null);
    }

    @Override
    public Page listPage(PositionPage basePage) {
        Page<Position> page = new Page(basePage.getPageCurrent(), basePage.getPageSize());
        LambdaQueryWrapper<Position> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(!StringUtils.isEmpty(basePage.getAddress()), Position::getAddress1, basePage.getAddress())
                .or().like(!StringUtils.isEmpty(basePage.getAddress()), Position::getAddress2, basePage.getAddress())
                .or().like(!StringUtils.isEmpty(basePage.getAddress()), Position::getAddress3, basePage.getAddress())
                .or().like(!StringUtils.isEmpty(basePage.getAddress()), Position::getAddressLast, basePage.getAddress());

        baseMapper.selectPage(page, queryWrapper);
        return page;
    }


}
